package factory 
{
	import drawing.DrawTool;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class BitmapFactory 
	{
		
		public static function createShapeBitmap(points:Array, texture:BitmapData):BitmapData
		{
			var shape:Shape = new Shape();
			//shape.graphics.lineStyle(1, 0x000000);
			shape.graphics.beginBitmapFill(texture);
			drawEdge(shape.graphics, points);
			shape.graphics.endFill();
			
			var sp:Sprite = new Sprite();
			sp.addChild(shape);
			shape.scaleY = -1;
			shape.y = shape.height;
			//shape.y = shape.height;
			//shape.x = shape.width
			var bm:BitmapData = new BitmapData(sp.width, sp.height, true, 0xFF0000);
			bm.draw(sp);
			return bm;
		}
		
		public static function createShapeColor(points:Array, color:uint, alpha:Number=1):BitmapData
		{
			var shape:Shape = new Shape();
			//shape.graphics.lineStyle(1, 0x000000);
			shape.graphics.beginFill(color, 1);
			DrawTool.drawEdge(shape.graphics, points);
			shape.graphics.endFill();
			var sp:Sprite = new Sprite();
			sp.addChild(shape);
			shape.scaleY = -1;
			shape.y = shape.height;
			//shape.y = shape.height;
			//shape.x = shape.width
			//trace("w: " + sp.width + " h: " + sp.height);
			shape.alpha = alpha;
			var bm:BitmapData = new BitmapData(sp.width, sp.height, true, 0xFF0000);
			bm.draw(sp);
			return bm;
		}
		
		public static function drawEdge(surface:Graphics, edgePoints:Array):void
		{
			var pointsXY:Array;
			var prevPointsXY:Array;
			for (var i:int = 0; i < edgePoints.length; i++ )
			{
				pointsXY = edgePoints[i];
				if (i == 0)
				{
					surface.moveTo(pointsXY[0], pointsXY[1]);
				}else if (pointsXY.length == 2)
				{
					surface.lineTo(pointsXY[0], pointsXY[1]);
				}else
				{
					surface.curveTo(pointsXY[2], pointsXY[3], pointsXY[0], pointsXY[1]);
				}
			}
		}
	}

}